package it.unisa.udocs.gestioneAutenticazione;

import it.unisa.udocs.commons.XMLParser;
import it.unisa.udocs.data.Account;
import it.unisa.udocs.facade.FacadeAutenticazione;
import it.unisa.udocs.facade.IFacadeAutenticazione;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom2.Document;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 * Servlet implementation class AllAccountServlet
 */

public class AllAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AllAccountServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		IFacadeAutenticazione facAut = new FacadeAutenticazione();
		ArrayList<Account> list=null;
		try {
			//Estrae dal database la lista di tutti i ricercatori
			list = facAut.getAllRicercatori();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// converte l'output in XML
		Document doc = XMLParser.ricercaAccount(list);
				
		// risponde al client
		XMLOutputter xml_out = new XMLOutputter();
		xml_out.setFormat(Format.getPrettyFormat());
		response.setContentType("text/xml");
		response.setHeader("Cache-Control",	"no-store, no-cache, must-revalidate");     
		PrintWriter out = response.getWriter(); 


		xml_out.output(doc, out);
	}

}
